# 批量读取数据

from pymysql import Connection

from cap14.data_define import Order

def write_orders(orders:list[Order],file_name:str):
    with open(file_name,'a',encoding="utf8") as f:
        for order in orders:
            f.write(str(order)+"\n")


if __name__ == '__main__':
    conn = Connection(
        host="192.168.0.88",
        port=3308,
        user="root",
        password="root"
    )
    conn.select_db("hmdp")

    cursor=conn.cursor()
    cursor.execute("select * from tb_order")
    rows=cursor.fetchall()

    orders=[]
    for row in rows:
        order=Order(order_id=row[0],date=str(row[1]),money=row[2],province=row[3])
        orders.append(order)

    conn.close()

    write_orders(orders,"../output/orders.txt")